import scrapy
from demo.items import DemoItem
import re
import logging


class EastmoneySpider(scrapy.Spider):
    name = 'mySpider'
    allowed_domains = ['eastmoney.com']

    def start_requests(self):
        url = "https://quote.eastmoney.com/center/gridlist.html#hs_a_board"
        yield scrapy.Request(url, self.parse)

    #对解析后的网页中的数据进行提取
    def parse(self, response):
        stock_rows = response.xpath('//table//tbody/tr[td[2]/a[contains(@href, "quote.eastmoney.com")]]')
        count = 0
        for row in stock_rows:
            item = DemoItem()
            item['stock_code'] = row.xpath('.//td[2]//a/text()').get(default='').strip()
            item['stock_name'] = row.xpath('.//td[3]//a/text()').get(default='').strip()

            # 提取价格数据
            item['last_price'] = row.xpath('.//td[5]//text()').get()
            item['change_percent'] = row.xpath('.//td[6]//text()').get(default='').strip()
            item['change_amount'] = row.xpath('.//td[7]//text()').get(default='').strip()
            item['volume'] = row.xpath('.//td[8]//text()').get(default='').strip()
            item['amplitude'] = row.xpath('.//td[10]//text()').get()
            item['high_price'] = row.xpath('.//td[11]//text()').get()
            item['low_price'] = row.xpath('.//td[12]//text()').get()
            item['open_price'] = row.xpath('.//td[13]//text()').get()
            item['close_price'] = row.xpath('.//td[14]//text()').get()
            count += 1
            yield item
